<?php

/* 
Plugin Name: 404 Error Log and SendMail
Plugin URI: http://www.traffyk.com/404-error-write-log-and-sendmail/
Description: Tracking of 404 error by saving log in a file.txt and send an mail to specified address (if selected).
Version: 0.2
Author: Traffyk
Author URI: http://www.traffyk.com

Copyright 2007 Traffyk  (email : traffyk@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

//Email address for sending message logs.
$traffyk404_email = "you@emailaddress.com";

//Path and filename for error file. By default log.txt is saved in root
$traffyk404_path = "log.txt";

//Subject for mail
$traffyk404_subject = "File not found";

// Send Mail, true or false
$traffyk404_emailSend = false;
// Write log file,  true or false
$traffyk404_log = true;
// Date Format, see http://www.php.net/date for another style
$traffyk404_date=date("D j M G:i:s T Y");

// Do not modify code after this line if you don't know what you're doing
##########################################

$traffyk404_date=date("D j M G:i:s T Y");

function traffyk404_writeLog() {
	global $traffyk404_path, $traffyk404_date;

	$message = "404, [$traffyk404_date], [client: $_SERVER[REMOTE_ADDR] ($_SERVER[HTTP_USER_AGENT])], $_SERVER[REQUEST_URI], $_SERVER[HTTP_REFERER]\n";
    $fp = fopen ($traffyk404_path,"a+");
    fwrite($fp, $message);
    fclose($fp);
}

function traffyk404_sendMail() {
	global $traffyk404_date, $traffyk404_subject, $traffyk404_email;
	
	 $message = "
------------------------------------------------------------------------------
Site:\t".get_bloginfo('name')." (".get_option('home').")
Error:\t404 $traffyk404_subject
Date:\t$traffyk404_date
Url:\t".get_settings('home')."$_SERVER[REQUEST_URI]
Ip address:\t$_SERVER[REMOTE_ADDR]
Browser:\t$_SERVER[HTTP_USER_AGENT]
Referer page:\t$_SERVER[HTTP_REFERER]
------------------------------------------------------------------------------";
    mail("$traffyk404_email", "[ Errore del server: $traffyk404_subject", $message,
      "From: error404@$SERVER_NAME\r\n"
      ."X-Mailer: PHP/" . phpversion());
}
	

function traffyk404() {
	global $traffyk404_emailSend, $traffyk404_log;
	
	if ($traffyk404_emailSend)
		traffyk404_sendMail();
	if ($traffyk404_log)
		traffyk404_writeLog();

		if (file_exists(TEMPLATEPATH . '/404.php'))
		require_once(TEMPLATEPATH . '/404.php');
	else
		require_once(TEMPLATEPATH . '/index.php');



	die();
}

add_filter('404_template', 'traffyk404');

?>